package com.zhao.service.serviceImpl;

import com.zhao.Others.DateAndTimeObject;
import com.zhao.dao.*;
import com.zhao.pojo.*;
import com.zhao.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class WalletServiceImpl implements WalletService {

    @Autowired
    private WalletDao walletDao;

    @Autowired
    private OrderFormDao orderFormDao;

    @Autowired
    private TradeRecordsDao tradeRecordsDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private EbickDao ebickDao;

    @Autowired
    private EbickGoodsDao ebickGoodsDao;

    @Override
    public String getMyWallet(Student student, HttpSession session) {

        Wallet wallet = walletDao.findOne(student.getStudentID(),student.getUniversityID());
        System.out.println("wallet "+wallet);
        if(wallet==null){
            return "没有钱包";
        }
        List<TradeRecords> tradeRecordsList = tradeRecordsDao.findByStudentID(student.getStudentID(),student.getUniversityID());
        List<StudentPayRecordsImpl> studentPayRecordsList = new ArrayList<>();
        for(TradeRecords tradeRecords : tradeRecordsList){
            StudentPayRecordsImpl studentPayRecords = new StudentPayRecordsImpl();
            studentPayRecords.setTradeRecords(tradeRecords);
            studentPayRecords.setType(tradeRecords.getPayerID().equals(student.getStudentID())?"支出":"收入");
            studentPayRecordsList.add(studentPayRecords);
        }
        session.setAttribute("payRecords",studentPayRecordsList);
        session.setAttribute("myWallet",wallet);

        return "success";
    }

    @Override
    public String studentCreatOneWallet(Student student, String password, HttpSession session) {

        Wallet wallet = new Wallet();
        wallet.setPassword(password);
        wallet.setAmount(0);
        wallet.setStudentID(student.getStudentID());
        wallet.setUniversityID(student.getUniversityID());
        System.out.println(wallet);
        int res = walletDao.add(wallet);
        if(res!=1){
            return "添加失败";
        }

        return "success";
    }

    @Override
    public String studentPay(Student student, String orderFormID, String payPassword, HttpSession session) {

        Wallet wallet = walletDao.findOne(student.getStudentID(),student.getUniversityID());
        if(wallet==null){
            System.out.println("查找钱包失败");
            return "系统错误";
        }

        OrderForm orderForm = orderFormDao.findByID(orderFormID,student.getUniversityID());
        if(orderForm==null){
            System.out.println("查找订单失败");
            return "系统错误";
        }

        if(!payPassword.equals(wallet.getPassword())){
            return "密码错误";
        }
        Double price = new Double(orderForm.getPrice());
        System.out.println(price);
        double amount = wallet.getAmount();
        if(amount<price){
            return "toInvest";
        }

        double newAmount = amount - price;

        Wallet universityWallet = walletDao.findOne(student.getUniversityID(),student.getUniversityID());
        if(universityWallet == null){
            System.out.println("查找学校账户失败");
            return "系统错误";
        }

        double universityNewAmount = universityWallet.getAmount()+price;

        wallet.setAmount(newAmount);
        universityWallet.setAmount(universityNewAmount);

        int res = walletDao.update(wallet);
        if(res!=1){
            System.out.println("修改余额失败");
            return "系统错误";
        }
        res = walletDao.update(universityWallet);
        if(res!=1){
            System.out.println("修改学校账户失败");
            return "系统错误";
        }

        orderForm.setStatus("待收货");
        res = orderFormDao.updateStatus(orderForm);
        if(res!=1){
            System.out.println("修改订单状态失败");
            return "系统错误";
        }

        /////添加交易记录！！！
        TradeRecords tradeRecords = new TradeRecords();
        tradeRecords.setAmount(price);
        tradeRecords.setOrderFormID(orderFormID);
        tradeRecords.setPayerID(student.getStudentID());
        tradeRecords.setReceiverID(student.getUniversityID());
        tradeRecords.setUniversityID(student.getUniversityID());
        tradeRecords.setPayTime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());

        res = tradeRecordsDao.add(tradeRecords);
        if(res!=1){
            System.out.println("添加交易记录失败");
            return "系统错误";
        }

        return "success";
    }

    @Override
    public String studentInvest(Student student, String amount, HttpSession session) {

        Wallet wallet = walletDao.findOne(student.getStudentID(),student.getUniversityID());

        Double oldAmount = wallet.getAmount();
        Double addAmount = new Double(amount);
        Double newAmount = oldAmount + addAmount;

        wallet.setAmount(newAmount);
        int res = walletDao.update(wallet);
        if(res!=1){
            return "系统错误，充值失败";
        }

        return "success";
    }

    @Override
    public String studentBackMoney(Student studnt, String orderFormID, HttpSession session) {
        System.out.println(orderFormID);
        OrderForm orderForm = orderFormDao.findByID(orderFormID,studnt.getUniversityID());
        if(orderForm==null){
            return "订单查询错误";
        }
        orderForm.setStatus("已退款");

        Wallet wallet = walletDao.findOne(studnt.getStudentID(),studnt.getUniversityID());
        Wallet universityWallet = walletDao.findOne(studnt.getUniversityID(),studnt.getUniversityID());
        if(wallet==null||universityWallet==null){
            return "查询钱包错误";
        }

        Double price = new Double(orderForm.getPrice());
        wallet.setAmount(wallet.getAmount()+price);
        universityWallet.setAmount(universityWallet.getAmount()-price);

        int res = orderFormDao.updateStatus(orderForm);
        if(res != 1){
            return "更新订单错误";
        }
        res = walletDao.update(wallet);
        if(res != 1){
            return "更新钱包错误";
        }
        res = walletDao.update(universityWallet);
        if(res!=1){
            return "更新钱包错误";
        }

        ///生成交易记录
        TradeRecords tradeRecords = new TradeRecords();
        tradeRecords.setPayTime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        tradeRecords.setUniversityID(studnt.getUniversityID());
        tradeRecords.setReceiverID(studnt.getStudentID());
        tradeRecords.setPayerID(studnt.getUniversityID());
        tradeRecords.setOrderFormID(orderFormID);
        tradeRecords.setAmount(price);
        res = tradeRecordsDao.add(tradeRecords);
        if(res!=1){
            return "系统错误";
        }

        return "success";
    }

    @Override
    public String studentYesReceiveEbick(Student studnt, String orderFormID, HttpSession session) {
        System.out.println(orderFormID);
        OrderForm orderForm = orderFormDao.findByID(orderFormID,studnt.getUniversityID());
        if(orderForm==null){
            return "订单查询错误";
        }
        orderForm.setStatus("已完成");
        Student seller = studentDao.findByStudentID(orderForm.getSellerID(),orderForm.getUniversityID());
        Student buyer = studentDao.findByStudentID(orderForm.getBuyerID(),orderForm.getUniversityID());
        seller.setEbickAmount(seller.getEbickAmount()-1);
        buyer.setEbickAmount(buyer.getEbickAmount()+1);
        int r = studentDao.updateStudent(seller);
        int rr = studentDao.updateStudent(buyer);
        if(r!=1 || rr!=1){
            System.out.println("更新学生电动车数量失败");
            return "系统错误";
        }


        Wallet wallet = walletDao.findOne(seller.getStudentID(),seller.getUniversityID());
        Wallet universityWallet = walletDao.findOne(studnt.getUniversityID(),studnt.getUniversityID());
        if(wallet==null||universityWallet==null){
            return "查询钱包错误";
        }

        Double price = new Double(orderForm.getPrice());
        wallet.setAmount(wallet.getAmount()+price);
        universityWallet.setAmount(universityWallet.getAmount()-price);

        int res = orderFormDao.updateStatus(orderForm);
        if(res != 1){
            return "更新订单错误";
        }
        res = walletDao.update(wallet);
        if(res != 1){
            return "更新钱包错误";
        }
        res = walletDao.update(universityWallet);
        if(res!=1){
            return "更新钱包错误";
        }

        ///生成交易记录
        TradeRecords tradeRecords = new TradeRecords();
        tradeRecords.setPayTime(DateAndTimeObject.getNowDate()+" "+DateAndTimeObject.getNowTime());
        tradeRecords.setUniversityID(studnt.getUniversityID());
        tradeRecords.setReceiverID(seller.getStudentID());
        tradeRecords.setPayerID(studnt.getUniversityID());
        tradeRecords.setOrderFormID(orderFormID);
        tradeRecords.setAmount(price);
        res = tradeRecordsDao.add(tradeRecords);
        if(res!=1){
            return "系统错误";
        }

        //修改电动车车主
        Ebick ebick = ebickDao.findByID(orderForm.getEbickID(),orderForm.getUniversityID());
        if(ebick==null){
            return "系统错误";
        }
        ebick.setNowOwnerID(studnt.getStudentID());
        ebick.setStatus("无证");
        res = ebickDao.updateOwner(ebick);
        if(res!=1){
            return "系统错误";
        }
        res = ebickDao.updateStatus(ebick);
        if(res!=1){
            return "系统错误";
        }
        ///修改商品状态
        EbickGoods ebickGoods = ebickGoodsDao.findByEbickID(ebick.getEbickID(),ebick.getUniversityID());
        if(ebickGoods==null){
            return "系统错误";
        }
        ebickGoods.setStatus("已完成");
        res = ebickGoodsDao.update(ebickGoods);
        if(res!=1){
            return "系统错误";
        }

        return "success";
    }
}
